<?php
/**
 * Created by JetBrains PhpStorm.
 * User: CAIXUDONG
 * Date: 11-6-16
 * Time: 下午4:06
 */
 
include '../bootstrap.php';

$_user = Model::getInstance('user');
$user_info_l = $_user->needLogin();

$user_id = $_input->post('user_id');
$password = $_input->post('password');

$user_info = $user_id == $user_info['user_id'] ? $user_info_l : $_user->getUserById($user_id);

$_password = substr(md5($password.PASSWORD_PREFIX),0,16);

$is_super_admin = Model::getInstance('user')->isSuperAdmin($user_info_l['user_id']);

if(!$is_super_admin && (!$password || $user_info['password'] != $_password)){
    $data = array(
        "statusCode"=>"300",
        "message"=>"密码错误",
    );
    echo Response::JSON($data);
    exit;
}

$newpassword = $_input->post('newpassword');
$repassword = $_input->post('repassword');

if($password && $newpassword != $repassword){
    $data = array(
        "statusCode"=>"300",
        "message"=>"两次输入的密码不一致",
    );
    echo Response::JSON($data);
    exit;
}

$user_name = $is_super_admin ? $_input->post('user_name') : $user_info['user_name'];
$email = $_input->post('email');
$tel = $_input->post('tel');
$real_name = $_input->post('real_name');
$svn_user = $_input->post('svn_user');

$ret = Model::getInstance('user')->updateUser($user_id,$user_name,$newpassword,$email,$tel,$real_name,$svn_user);

$role_ids = $_input->post('role_ids',false);

if($ret){
    if($role_ids){
        $role_ids = explode(',',$role_ids);
        $role_ids = array_unique($role_ids);
        Model::getInstance('userhasrole')->delByUserId($user_id);
        foreach($role_ids as $v){
            if($v){
                Model::getInstance('userhasrole')->add($user_id,$v);
            }
        }
    }
    $data = array(
        "statusCode"=>"200",
        "message"=>"修改成功",
        "navTabId"=>"user",
    );
    echo Response::JSON($data);
}else{

    $data = array(
        "statusCode"=>"300",
        "message"=>"修改失败",
    );
    echo Response::JSON($data);
}